#!/usr/bin/env python
# -*- coding: utf-8 -*-
import cexapi
import datetime
import time
import re

credentialFile = open('credentials.txt', 'r+')

for line in credentialFile:
        resultLine = re.split(';', line)  
        if resultLine[0]=='hannesh':
            apiUser=resultLine[0]
            apiCred1=resultLine[1]
            apiCred2=resultLine[2]      

demo = cexapi.api(apiUser, apiCred1, apiCred2)

def GHS2NMC2GHS():
    #print "\nsell GHS, buy BTC"
    #print "Order book (GHS/BTC)"
    order_bookGHSvBTC= demo.order_book() ## or demo.order_book('GHS/BTC')    
    #print order_book
    #print "asks:",order_book['asks']
    #print "best bid:",order_bookGHSvBTC['bids'][0]
    btcProGHSLast=float(order_bookGHSvBTC['bids'][0][0]) 
    currBTC=btcProGHSLast*10
    #print "get", currBTC, "BTC"

    #print "sell BTC, buy NMC"
    order_bookNMCvBTC = demo.order_book("NMC/BTC")    
    #print "best ask:",order_bookNMCvBTC['asks'][0]
    btcProNMCLast=float(order_bookNMCvBTC['asks'][0][0])
    currNMC=currBTC/btcProNMCLast
    #print "get", currNMC, "NMC"      
    #print nmcVersusBTC
    
    #print "sell NMC, buy GHS"
    order_bookGHSvNMC = demo.order_book("GHS/NMC")    
    #print "best ask:",order_bookGHSvNMC['asks'][0]
    nmcProGHSLast=float(order_bookGHSvNMC['asks'][0][0])
    #print "nmcProGHSLast=",nmcProGHSLast
    curr2GHS=currNMC/nmcProGHSLast
    #print "get", curr2GHS, "GHS"      
    #print nmcVersusBTC
    
    return [curr2GHS]

def GHS2NMC2GHSReverse():    
    
    #print "\nsell GHS, buy NMC"
    order_bookGHSvNMC = demo.order_book("GHS/NMC")    
    #print "best bid:",order_bookGHSvNMC['bids'][0]
    nmcProGHSLast=float(order_bookGHSvNMC['bids'][0][0])
    #print "nmcProGHSLast=",nmcProGHSLast
    currNMC=nmcProGHSLast*10
    #print "get ",currNMC, "NMC"
    
    #print "sell NMC, buy BTC"
    order_bookNMCvBTC = demo.order_book("NMC/BTC")    
    #print "best bid:",order_bookNMCvBTC['bids'][0]
    btcProNMCLast=float(order_bookNMCvBTC['bids'][0][0])
    currBTC=currNMC*btcProNMCLast
    #print "get", currBTC, "BTC" 
    
    #print "sell BTC, buy GHS"    
    order_bookGHSvBTC= demo.order_book() ## or demo.order_book('GHS/BTC')  
    #print "best ask:",order_bookGHSvBTC['asks'][0]
    btcProGHSLast=float(order_bookGHSvBTC['asks'][0][0]) 
    curr2GHS=currBTC/btcProGHSLast
    #print "get", curr2GHS, "GHS"
    
    return [curr2GHS]
    

while True:    
    try: 
        curr2GHSNormal=GHS2NMC2GHS()[0]
        curr2GHSReverse=GHS2NMC2GHSReverse()[0]
        #print curr2GHSNormal
        #print curr2GHSReverse
    
        if curr2GHSNormal>=10.01 or curr2GHSReverse>=10.01:
            print (time.strftime("%H:%M:%S"))
            print "ARBITRAGE curr2GHSNormal", curr2GHSNormal
            print "ARBITRAGE curr2GHSReverse", curr2GHSReverse
            arbitrageFound=1
        else:
            arbitrageFound=0

        if arbitrageFound:
            print "no Sleep"
        else:
            time.sleep(20)
    except:
        print "general Exception"
        time.sleep(60)
    